package com.gkd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkd.context.BaseContext;
import com.gkd.pojo.dto.CommentDTO;
import com.gkd.pojo.dto.CommentPageQueryDTO;
import com.gkd.pojo.entity.Comment;
import com.gkd.pojo.vo.CommentVO;
import com.gkd.result.PageResult;
import com.gkd.service.CommentService;
import com.gkd.mapper.CommentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author ZJF
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2024-06-15 18:18:13
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 添加评论
     * @param commentDTO
     */
    @Override
    public void add(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);

        //设置发送人员
        comment.setUserId(BaseContext.getCurrentId());

        //设置发送时间
        comment.setCreateTime(new Date());

        //插入
        commentMapper.insert(comment);
    }

    /**
     * 修改评论
     * @param commentDTO
     */
    @Override
    public void update(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);

        //设置发送时间
        comment.setCreateTime(new Date());

        commentMapper.updateById(comment);
    }

    /**
     * 评论分页查询
     * @param commentPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CommentPageQueryDTO commentPageQueryDTO) {
        Page<CommentVO> page = new Page<>(commentPageQueryDTO.getCurrent(), commentPageQueryDTO.getPageSize());
        IPage<CommentVO> commentPage =commentMapper.selectCommentPage(page,commentPageQueryDTO);
        return new PageResult(commentPage.getTotal(),commentPage.getRecords());
    }
}




